<template>
  <div class="comment-list">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <comment-item
        v-for="comment in currentVideoCommentList"
        :comment='comment'
        :row="3"
        :key="comment._id"
        :reply=true
        @onreplyRecomment='onreplyRecomment'
        @onUpdateComment="onUpdateComment"
      >
      </comment-item>
    </van-list>
  </div>
</template>

<script>
import commentItem from '@/components/comment-item'
import globalBus from '@/utils/global-bus'
import { getVideoComment } from '@/api/video'

export default {
  name: 'CommentList',
  components: {
    commentItem
  },
  props: {
    videoid: {
      required: true
    }
  },

  data () {
    return {
      finished: false,
      loading: false,
      currentVideoCommentList: [],
      currentCommentPage: 0
    }
  },
  computed: {},
  watch: {},
  created () {
    globalBus.$on('clearComment', () => {
      this.currentVideoCommentList = []
      this.currentCommentPage = 0
      this.finished = false
      this.loading = true
    })
    globalBus.$on('onLoadComment', () => {
      this.onLoad()
    })

    globalBus.$on('updataComment', (comment) => {
      this.currentVideoCommentList = []
      this.currentCommentPage = 0
      this.finished = false
      this.loading = true
      this.onLoad()
    })
  },
  mounted () {
  },
  activated () {

  },
  methods: {
    async onLoad () {
      try {
        this.currentCommentPage++
        const res = await getVideoComment(this.videoid, this.currentCommentPage)
        this.currentVideoCommentList.push(...res.data.data)
        this.$emit('onLoadComment')
        this.loading = false
        if (!res.data.data.length) {
          this.finished = true
        }
      } catch (error) {
        this.currentCommentPage--
      }
    },
    async onreplyRecomment (commentid, name, type) {
      this.$emit('listenReplyRecomment', commentid, name, type)
    },
    async onUpdateComment () {
      this.currentVideoCommentList = []
      this.currentCommentPage = 0
      this.finished = false
      this.loading = true
      this.onLoad()
      this.$emit('onUpdateComment')
    }
  }
}
</script>

<style scoped lang="less">
.van-cell {
  background-color: #fff !important;
}
</style>
